package com.android.lovely.room.chat.database

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query

@Dao
interface ChatInfoDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertMessage(message: CacheMessage)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertMessages(message: List<CacheMessage>)

    @Query("delete from cachemessage where id=:messageId")
    suspend fun deleteMessage(messageId: Int)

    @Query("update cachemessage set question='' where id=:messageId")
    suspend fun deleteSendMessage(messageId: Int)

    @Query("update cachemessage set reply='' where id=:messageId")
    suspend fun deleteReceivedMessage(messageId: Int)

    @Query("delete from cachemessage where bot_id=:botId")
    suspend fun deleteMessages(botId: Int)

    @Query("select * from cachemessage where bot_id=:botId and id<:messageId and (question != '' OR reply != '') order by id DESC limit :loadSize")
    suspend fun getMessages(botId: Int, messageId: Int, loadSize: Int): List<CacheMessage>

    @Query("select * from cachemessage where id=:messageId")
    suspend fun getMessage(messageId: Int): CacheMessage?
}